package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.ExamRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 考试记录Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface ExamRecordMapper extends BaseMapper<ExamRecord> {

    /**
     * 根据考试ID查询考试记录列表
     * 
     * @param examId 考试ID
     * @return 考试记录列表
     */
    @Select("SELECT * FROM exam_record WHERE exam_id = #{examId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<ExamRecord> selectByExamId(@Param("examId") String examId);

    /**
     * 根据用户ID查询考试记录列表
     * 
     * @param userId 用户ID
     * @return 考试记录列表
     */
    @Select("SELECT * FROM exam_record WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<ExamRecord> selectByUserId(@Param("userId") String userId);

    /**
     * 根据考试ID和用户ID查询考试记录
     * 
     * @param examId 考试ID
     * @param userId 用户ID
     * @return 考试记录
     */
    @Select("SELECT * FROM exam_record WHERE exam_id = #{examId} AND user_id = #{userId} AND is_deleted = 0")
    ExamRecord selectByExamIdAndUserId(@Param("examId") String examId, @Param("userId") String userId);

    /**
     * 根据状态查询考试记录列表
     * 
     * @param status 状态
     * @return 考试记录列表
     */
    @Select("SELECT * FROM exam_record WHERE status = #{status} AND is_deleted = 0 ORDER BY create_time DESC")
    List<ExamRecord> selectByStatus(@Param("status") Integer status);

    /**
     * 分页查询考试记录列表
     * 
     * @param page 分页参数
     * @param examId 考试ID
     * @param userId 用户ID
     * @param userName 用户姓名（模糊查询）
     * @param status 状态
     * @return 分页结果
     */
    IPage<ExamRecord> selectRecordPage(Page<ExamRecord> page,
                                      @Param("examId") String examId,
                                      @Param("userId") String userId,
                                      @Param("userName") String userName,
                                      @Param("status") Integer status);

    /**
     * 查询考试排行榜
     * 
     * @param examId 考试ID
     * @param limit 限制数量
     * @return 排行榜列表
     */
    @Select("SELECT * FROM exam_record WHERE exam_id = #{examId} AND status IN (2, 3) AND is_deleted = 0 ORDER BY user_score DESC, duration_seconds ASC LIMIT #{limit}")
    List<ExamRecord> selectRanking(@Param("examId") String examId, @Param("limit") Integer limit);

    /**
     * 统计考试记录数量
     * 
     * @param examId 考试ID
     * @param status 状态
     * @return 记录数量
     */
    @Select("SELECT COUNT(*) FROM exam_record WHERE exam_id = #{examId} AND status = #{status} AND is_deleted = 0")
    Integer countByExamIdAndStatus(@Param("examId") String examId, @Param("status") Integer status);

    /**
     * 统计用户参与的考试数量
     * 
     * @param userId 用户ID
     * @return 考试数量
     */
    @Select("SELECT COUNT(*) FROM exam_record WHERE user_id = #{userId} AND is_deleted = 0")
    Integer countByUserId(@Param("userId") String userId);

    /**
     * 查询考试的平均分
     * 
     * @param examId 考试ID
     * @return 平均分
     */
    @Select("SELECT AVG(user_score) FROM exam_record WHERE exam_id = #{examId} AND status IN (2, 3) AND is_deleted = 0")
    BigDecimal selectAvgScoreByExamId(@Param("examId") String examId);

    /**
     * 查询考试的最高分
     * 
     * @param examId 考试ID
     * @return 最高分
     */
    @Select("SELECT MAX(user_score) FROM exam_record WHERE exam_id = #{examId} AND status IN (2, 3) AND is_deleted = 0")
    BigDecimal selectMaxScoreByExamId(@Param("examId") String examId);

    /**
     * 查询考试的最低分
     * 
     * @param examId 考试ID
     * @return 最低分
     */
    @Select("SELECT MIN(user_score) FROM exam_record WHERE exam_id = #{examId} AND status IN (2, 3) AND is_deleted = 0")
    BigDecimal selectMinScoreByExamId(@Param("examId") String examId);

    /**
     * 查询考试的通过人数
     * 
     * @param examId 考试ID
     * @param passScore 及格分数
     * @return 通过人数
     */
    @Select("SELECT COUNT(*) FROM exam_record WHERE exam_id = #{examId} AND user_score >= #{passScore} AND status IN (2, 3) AND is_deleted = 0")
    Integer countPassedByExamId(@Param("examId") String examId, @Param("passScore") Integer passScore);

    /**
     * 查询超时的考试记录
     * 
     * @return 超时的考试记录列表
     */
    List<ExamRecord> selectTimeoutRecords();

    /**
     * 查询用户的考试统计信息
     * 
     * @param userId 用户ID
     * @return 统计信息
     */
    ExamRecord selectUserStatistics(@Param("userId") String userId);
}
